var __bind = function(fn, me) { 
	return function() { 
		return fn.apply(me, arguments); 
	}; 
},
 __slice = [].slice;

(function($, window) {
    var ResizableColumns;

    ResizableColumns = (function() {
        ResizableColumns.prototype.defaults = {
            store: window.store,
            rigidSizing: false
        };
        
        function ResizableColumns($table, options) {
            this.mousedown = __bind(this.mousedown, this);
            var _this = this;
            
            this.options = $.extend({}, this.defaults, options);
            this.$table = $table;
            this.tableId = this.$table.data('resizable-columns-id');
            this.createHandles();
            this.restoreColumnWidths();
            this.syncHandleWidths();
            $(window).on('resize.rc', (function() {
                return _this.syncHandleWidths();
            }));
        }
        
        ResizableColumns.prototype.destroy = function() {
            this.$handleContainer.remove();
            this.$table.removeData('resizableColumns');
            return $(window).off('.rc');
        };
        
        ResizableColumns.prototype.createHandles = function() {
            var _this = this;
        
            this.$table.before((this.$handleContainer = $("<div class='rc-handle-container' />")));
            this.$table.find('tr th').each(function(i, el) {
                var $handle;
        
                if (_this.$table.find('tr th').eq(i + 1).length === 0 || (_this.$table.find('tr th').eq(i).attr('data-noresize') != null) || (_this.$table.find('tr th').eq(i + 1).attr('data-noresize') != null)) {
                    return;
                }
                $handle = $("<div class='rc-handle' />");
                $handle.data('th', $(el));
                return $handle.appendTo(_this.$handleContainer);
            });
            return this.$handleContainer.on('mousedown', '.rc-handle', this.mousedown);
        };
        
        ResizableColumns.prototype.syncHandleWidths = function() {
            var _this = this;
        
            this.$handleContainer.width(this.$table.width());
            return this.$handleContainer.find('.rc-handle').each(function(_, el) {
                return $(el).css({
                    left: $(el).data('th').outerWidth() + ($(el).data('th').offset().left - _this.$handleContainer.offset().left),
                    height: _this.$table.height()
                });
            });
        };
        
        ResizableColumns.prototype.saveColumnWidths = function() {
            var _this = this;
        
            return this.$table.find('tr th').each(function(_, el) {
                var id;
        
                if ($(el).attr('data-noresize') == null) {
	                id = _this.tableId + '-' + $(el).data('resizable-column-id');
	                if (_this.options.store != null) {
	                    return store.set(id, $(el).width());
	                }
                }
            });
        };
        
        ResizableColumns.prototype.restoreColumnWidths = function() {
            var _this = this;
        
            return this.$table.find('tr th').each(function(_, el) {
                var id, width;
        
                id = _this.tableId + '-' + $(el).data('resizable-column-id');
                if ((_this.options.store != null) && (width = store.get(id))) {
                    return $(el).width(width);
                }
            });
        };
        
        ResizableColumns.prototype.mousedown = function(e) {
            var $currentGrip, $leftColumn, $rightColumn, idx, leftColumnStartWidth, rightColumnStartWidth,
              _this = this;
            
            e.preventDefault();
            this.startPosition = e.pageX;
            $currentGrip = $(e.currentTarget);
            $leftColumn = $currentGrip.data('th');
            leftColumnStartWidth = $leftColumn.width();
            idx = this.$table.find('tr th').index($currentGrip.data('th'));
            $rightColumn = this.$table.find('tr th').eq(idx + 1);
            rightColumnStartWidth = $rightColumn.width();
            $(document).on('mousemove.rc', function(e) {
                var difference, newLeftColumnWidth, newRightColumnWidth;
                
                difference = e.pageX - _this.startPosition;
                newRightColumnWidth = rightColumnStartWidth - difference;
                newLeftColumnWidth = leftColumnStartWidth + difference;
                if (
                		_this.options.rigidSizing 
                		&& 
                		(
                			(parseInt($rightColumn[0].style.width) < $rightColumn.width()) 
                			&& 
                			(newRightColumnWidth < $rightColumn.width())
                		) 
                		|| 
                		(
                			(parseInt($leftColumn[0].style.width) < ($leftColumn.width() - 20)) 
                			&& 
                			(newLeftColumnWidth < ($leftColumn.width() - 20))
                		)
                	) {
                    return;
                }
                //超出部分显示。。。
                var $tr = _this.$table.find('tr');
                $.each($tr, function(index, obj) {
                    if ($(obj).find('td')) {           
                        $(obj).find('td').eq(idx).find('div').attr({'class':'text-overflow','style':'width:' + newLeftColumnWidth + 'px'});               
                    } 
                });
                
                $leftColumn.width(newLeftColumnWidth);
                $rightColumn.width(newRightColumnWidth);
                return _this.syncHandleWidths();
            });
          
            return $(document).one('mouseup', function() {
                $(document).off('mousemove.rc');
                return _this.saveColumnWidths();
            });
            
            
        };

        return ResizableColumns;

    })();
  
    return $.fn.extend({
    	resizableColumns: function() {
	        var args, option;
	
	        option = arguments[0], args = 2 <= arguments.length ? __slice.call(arguments, 1) : [];
	        return this.each(function() {
	            var $table, data;
	
	            $table = $(this);
	            data = $table.data('resizableColumns');
	            if (!data) {
	                $table.data('resizableColumns', (data = new ResizableColumns($table, option)));
	            }
	            if (typeof option === 'string') {
	                return data[option].apply(data, args);
	            }
	        });
    	}
    });
    
})(window.jQuery, window);
var tableResizable;
initTabTable = function(table) {
	$targetElem = table;
//	if($targetElem.hasClass("rz-col")) {
    	var thisTableWidth = $targetElem.width();
    	thisTableWidth = thisTableWidth - 300;
    	
    	var width_arr = [];
    	var thead_length = 0;
    	
    	var thead_count = $targetElem.find('thead tr:first').find('th').length;
    	
    	var opera_length = 0;
    	
    	$targetElem.find('thead tr:first').find('th').each(function(index, obj) {
    		if(index + 1 < thead_count) {
    			thead_length += $(obj).text().length;
    		} else {
    			opera_length = parseInt($(obj).width());
    		}
    	});
    	$targetElem.find('thead tr:first').find('th').each(function(index, obj) {
    		if(index + 1 < thead_count) {
    			width_arr.push($(obj).text().length / thead_length * (thisTableWidth - opera_length));
    		} else {
    			width_arr.push(opera_length);
    		}
    	});
    	
    	//所有表格td内添加div
    	$targetElem.find('tbody tr td').each(function(index, obj) {
    		if($targetElem.find('.no-records-found').length > 0) {
    			return;
    		}
    		if($(obj).find('div.text-overflow').length) {
    			return;
    		}
    		if($(obj).find('input[type=checkbox]').length) {
    			return;
    		}
            var $html = $(obj).html();
            var $text = $(obj).text();
            if((index + 1) % thead_count == 0) {
            	$(obj).html('<div class="text-overflow" style="min-width:' + width_arr[index % thead_count] + 'px;">' + $html + '</div>');
            	return;
            } else {
            	$(obj).html('<div class="text-overflow" style="width:' + width_arr[index % thead_count] + 'px;" title="' + $text + '">' + $html + '</div>');
            }
    	});
    	try{
    		if(tableResizable){
    			tableResizable.resizableColumns('destroy');
        	}	
    	}catch(e){
    		//console.log("销毁失败");
    	}
    	//调用表格拖拽插件
    	tableResizable=$targetElem.resizableColumns({});
//	}
	var $sumWidth = 0;
	$targetElem.parent().find('.rc-handle-container').width($targetElem.width());
	$targetElem.parent().find('.rc-handle-container').find("div.rc-handle").each(function(i, o) {
        $(o).css({
            left: $(o).data('th').outerWidth() + ($(o).data('th').offset().left - $(o).parent().offset().left),
            height: $targetElem.height()
        });
	});
};
